package com.dididoctor.doctor.Activity.Order.DiagnosisSuggest;

import android.content.Intent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.TextView;

import com.dididoctor.doctor.Bean.SuggestBean;
import com.dididoctor.doctor.MV.EduActivity;
import com.dididoctor.doctor.R;
import com.dididoctor.doctor.Ui.customlistview.OnRefreshListener;
import com.dididoctor.doctor.Ui.customlistview.RefreshListView;
import com.dididoctor.doctor.Utils.MyToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liying on 2016/4/25.
 */
public class SuggestListActivity extends EduActivity implements SuggestListView {
    private RefreshListView mLvSuggest;
    private ArrayList<SuggestBean> data = new ArrayList<SuggestBean>();
    private SuggestAdapter adapter = null;
    private SuggestListPresenter mPreseneter;
    private int pageIndex=1;
    private SuggestBean tagBean = null;
    private Button mBtnConfirm;
    private boolean isChoose = false;
    private String disId;


    @Override
    public void init() {
        ((TextView) findViewById(R.id.tv_title)).setText("建议列表");
        mLvSuggest = (RefreshListView) findViewById(R.id.lv_suggest);

        mLvSuggest.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onPullDownRefresh() {
                pageIndex = 1;
                mPreseneter.getSuggestList("1",pageIndex,"1");
                mLvSuggest.onRefreshFinish();
            }

            @Override
            public void onLoadMoring() {
                pageIndex++;
                mPreseneter.getSuggestList("1",pageIndex,"1");
                mLvSuggest.onRefreshFinish();
            }

            @Override
            public void onScrollListener(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
        mLvSuggest.setHeadAndFoot(true, true);

        mPreseneter = new SuggestListPresenter(this,this);
        mPreseneter.getSuggestList("1",pageIndex,disId);

        mBtnConfirm = (Button) findViewById(R.id.btn_suggest_confirm);
        mBtnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isChoose) {
                    Intent intent = new Intent(SuggestListActivity.this, DiagnosisSuggestActivity.class);
                    intent.putExtra("data", tagBean);
                    setResult(RESULT_OK, intent);
                    SuggestListActivity.this.finish();
                }else{
                    MyToast.DefaultmakeText(SuggestListActivity.this,"请选择建议",0);
                }
            }
        });
    }

    @Override
    public void onClickEvent(View view) {

    }

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_suggestlist);
        disId = getIntent().getStringExtra("disId");
    }

    @Override
    public void dataChange(int position) {
        isChoose = true;
        for (int i = 0; i < data.size(); i++) {
            if (position == i) {
                data.get(i).setTag("0");
                tagBean = data.get(i);
            } else {
                data.get(i).setTag("1");
            }
        }
        adapter.notifyDataSetChanged();
    }


    @Override
    public void getDataFail() {
        mLvSuggest.onRefreshFinish();
    }

    @Override
    public void getDataSucced(List<SuggestBean> suggestBeans) {
        if(adapter==null) {
            adapter = new SuggestAdapter(SuggestListActivity.this, data);
            mLvSuggest.setAdapter(adapter);
        }
        if (suggestBeans != null && suggestBeans.size() > 0) {
            if (pageIndex == 1) {
                this.data.clear();
            }
            this.data.addAll(suggestBeans);
            adapter.notifyDataSetChanged();
        }
        mLvSuggest.onRefreshFinish();
    }

}
